package com.sunlei.webtest;

/**
 * Created by 001 on 2017/7/17.
 */

import com.sunlei.webtest.bean.User;
import com.sunlei.webtest.controller.img;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import com.sunlei.webtest.bean.test;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;

@SpringBootApplication
@RestController
public class WebTest {

    @Autowired
    private RabbitAdmin rabbitAdmin;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    WebTest(){
        System.out.println("[进行初始化]-----");
//        String exchangeName = "test.direct";
//        String queueName = "test.direct.queue";
//        String routingKey = "direct";
//        DirectExchange exchange = new DirectExchange(exchangeName,false,false);
        System.out.println(rabbitAdmin);
//        rabbitAdmin.declareExchange(exchange);
//        rabbitAdmin.declareExchange(new DirectExchange(exchangeName,false,false));
//        rabbitAdmin.declareQueue(new Queue(queueName,false));
//        rabbitAdmin.declareBinding(new Binding(queueName,
//                Binding.DestinationType.QUEUE,
//                exchangeName,routingKey,new HashMap<>()));
//        System.out.println("[rabbitmq建立成功]");
    }

    @RequestMapping("/")
    public String greeting() {
        System.out.println(rabbitAdmin);
        String exchangeName = "test.direct";
        String queueName = "test.direct.queue";
        String routingKey = "direct";
        DirectExchange exchange = new DirectExchange(exchangeName,false,false);
        rabbitAdmin.declareExchange(exchange);
        rabbitAdmin.declareExchange(new DirectExchange(exchangeName,false,false));
        rabbitAdmin.declareQueue(new Queue(queueName,false));
        rabbitAdmin.declareBinding(new Binding(queueName,
                Binding.DestinationType.QUEUE,
                exchangeName,routingKey,new HashMap<>()));
        System.out.println("[rabbitmq建立成功]");

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.getHeaders().put("desc","描述");
        messageProperties.getHeaders().put("type","自定义类型");
        Message message = new Message("test rabbitTemplate".getBytes(),messageProperties);

        rabbitTemplate.convertAndSend("beanExchange", "bean.test", message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                System.out.println("[MessagePostProcessor]");
                message.getMessageProperties().getHeaders().put("add","额外信息");
                return message;
            }
        });

        return "Hello World!";
    }

    @RequestMapping(value = "/loginbypost", method = RequestMethod.POST)
    @ResponseBody
    public String loginByPost(@RequestParam(value = "username", required = true) String name,
                              @RequestParam(value = "password", required = true) String pwd) {
        System.out.println("post is ok /loginbypost");
        class login4Return {
            String name;
            String pwd;

            public login4Return(String name, String pwd) {
                this.name = name;
                this.pwd = pwd;
            }

            @Override
            public String toString() {
                return "login4Return{" +
                        "name='" + name + '\'' +
                        ", pwd='" + pwd + '\'' +
                        '}';
            }
        }
        System.out.println(new login4Return(name, pwd).toString());
        return (new login4Return(name, pwd).toString());
    }

    @RequestMapping(value = "/loginbypost2", method = RequestMethod.POST)
    public User loginByPost2(@RequestParam(value = "username", required = true) String name,
                              @RequestParam(value = "password", required = true) String pwd) {
        System.out.println("post is ok /loginbypost2");
        User user = new User();
        user.setName("lijia");
        user.setAge(27);
        user.setAddress("北京");
        return user;
    }

    @RequestMapping(value = "/upload")
    public User upload() {
//        public User upload(@RequestParam("test") MultipartFile file) {
        System.out.println("post is ok /upload");
//        if (!file.isEmpty()) {
//            String fileName = file.getOriginalFilename();
//        }     // 获取文件名

        User user = new User();
        user.setName("lijia");
        user.setAge(27);
        user.setAddress("北京");
        return user;
    }

    @RequestMapping(value = "/getPic")
    public User getPic(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("post is ok /getPic");

        Image image = null;
        File sourceimage = new File("c:/1.png");
        image = ImageIO.read(sourceimage);
        String base64 = img.GetImageStrFromPath("c:/1.png");
        img.GenerateImage(base64);
        System.out.println(image);
        User user = new User();
        user.setName("lijia");
        user.setAge(27);
        user.setAddress(base64);
        return user;
    }

    @RequestMapping(value = "/getPic2")
    public User getPic(@RequestParam(value = "base64", required = true) String base64) throws IOException {
        System.out.println("post is ok /getPic");

        Image image = null;
        File sourceimage = new File("c:/1.png");
        image = ImageIO.read(sourceimage);
//        String base64 = img.GetImageStrFromPath("c:/1.png");
        img.GenerateImage(base64);
        System.out.println(image);
        User user = new User();
        user.setName("lijia");
        user.setAge(27);
        user.setAddress(base64);
        return user;
    }

    @RequestMapping(value = "/getFire")
    public String getFire() throws IOException {
        System.out.println("post is ok /getFire");

        String jsonStr = "{\n" +
                "    \"code\": 200,\n" +
                "    \"data\": {\n" +
                "        \"component\": {\n" +
                "            \"deviceAddr\": \"1-1-1\",\n" +
                "            \"id\": 1,\n" +
                "            \"imageId\": 1,\n" +
                "            \"imagePosition\": \"100,100\",\n" +
                "            \"position\": \"图书馆1楼楼梯口\",\n" +
                "            \"projectId\": 1,\n" +
                "            \"systemAddr\": \"1\",\n" +
                "            \"systemType\": 1,\n" +
                "            \"type\": 1\n" +
                "        },\n" +
                "        \"confirm\": 0,\n" +
                "        \"confirmDescription\": \"\",\n" +
                "        \"confirmTime\": null,\n" +
                "        \"confirmUser\": null,\n" +
                "        \"fireCode\": \"HJ1\",\n" +
                "        \"handle\": 0,\n" +
                "        \"id\": 1,\n" +
                "        \"project\": {\n" +
                "            \"address\": \"西安电子科技大学图书馆\",\n" +
                "            \"city\": \"西安市\",\n" +
                "            \"contact\": \"张三\",\n" +
                "            \"contactPhone\": \"16666666666\",\n" +
                "            \"created\": \"2017-06-09 11:53:23\",\n" +
                "            \"deviceId\": \"610000-0001\",\n" +
                "            \"district\": \"长安区\",\n" +
                "            \"gps\": \"108.9229969971,34.2372347149\",\n" +
                "            \"id\": 1,\n" +
                "            \"name\": \"西安电子科技大学图书馆\",\n" +
                "            \"outsideImg\": \"imgs/waiguantu1.jpg\",\n" +
                "            \"province\": \"陕西省\",\n" +
                "            \"secondContact\": null,\n" +
                "            \"secondContactPhone\": null,\n" +
                "            \"version\": 0\n" +
                "        },\n" +
                "        \"time\": \"2017-07-23 14:54:50\"\n" +
                "    },\n" +
                "    \"message\": \"返回火警信息成功\"\n" +
                "}";
        return jsonStr;
    }

    public static void main(String[] args) {
        SpringApplication.run(WebTest.class, args);
    }
}